package com.tensquare.friend.service;

import com.tensquare.entity.IdWorker;
import com.tensquare.friend.dao.FriendDao;
import com.tensquare.friend.dao.NoFriendDao;
import com.tensquare.friend.pojo.Friend;
import com.tensquare.friend.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * Created by WF on 2020/12/23 15:58
 */
@Service
@Transactional
public class FriendService {

    @Autowired
    private FriendDao friendDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private NoFriendDao noFriendDao;
    //1. 添加好友
    public int addFriend(String id, String friendid) {
        //1.1)根据用户id及好友id查询是否有记录
        int count = friendDao.selectCount(id,friendid);
        //1.2)判断是否有记录
        if(count > 0){
            return 0;
        }
        //1.3)构造一个好友对象
        Friend friend = new Friend();
        friend.setFriendid(friendid);
        friend.setUserid(id);
        friend.setIslike("0");
        //1.4)保存到数据库中
        friendDao.save(friend);

        //1.5)查询好友表中是否有添加我为好友的记录
        count = friendDao.selectCount(friendid, id);
        //1.6)判断是否有值
        if(count > 0){  //此时更新isLike的值
            friendDao.updateIsLike(id,friendid,"1");
            friendDao.updateIsLike(friendid,id,"1");
        }
        //1.7)返回
        return 1;
    }
    //2.删除好友
    public void delete(String userId, String friendid) {
        //2.1)从好友表中删除记录
        friendDao.deleteUser(userId,friendid);
        //2.2)更新islike的值为0
        friendDao.updateIsLike(friendid,userId,"0");
        //2.3)向非好友表添加一条记录
        NoFriend nofriend = new NoFriend(userId,friendid);
        noFriendDao.save(nofriend);
    }
}
